package initdb

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
	"gitee.com/zaiqiang231/lovesport-user-service/app/util"
)

type initSysAuthority struct {
	DbTable
}

func (i initSysAuthority) GetTable() store.DbTable {
	return model.SysAuthority{}
}

func (i initSysAuthority) InitData(ds *store.Datastore) {
	entities := []model.SysAuthority{
		{AuthorityId: 888, AuthorityName: "普通用户", ParentId: util.Pointer[uint](0), DefaultRouter: "dashboard"},
		{AuthorityId: 9528, AuthorityName: "测试角色", ParentId: util.Pointer[uint](0), DefaultRouter: "dashboard"},
		{AuthorityId: 8881, AuthorityName: "普通用户子角色", ParentId: util.Pointer[uint](888), DefaultRouter: "dashboard"},
	}
	err := ds.DB.Create(entities).Error
	if err != nil {
		baseLog.Errorf("initAuthority InitData Create fail, %v", err)
		return
	}
	//菜单关联到 角色
	var menus []model.SysBaseMenu
	err = ds.DB.Find(&menus).Error
	if err != nil {
		baseLog.Errorf("initAuthority InitData find menus fail, %v", err)
		return
	}

	for _, entity := range entities {
		err = ds.DB.Model(&entity).Association("SysBaseMenus").Replace(menus)
		if err != nil {
			baseLog.Errorf("initAuthority InitData update authority fail, %v", err)
			return
		}
	}

}
